#ifndef ERRORS_H
#define ERRORS_H

#include "pari-python.h"

int exception_handler(long val);

struct ExceptionKeeper {
	bool restored;
	jmp_buf __old_env;
	ExceptionKeeper();
	~ExceptionKeeper(); // desctructor to make sure handler is removed even on return statement
	void restore();
};

#define CATCH_PARI { \
  ExceptionKeeper __keeper; \
  VOLATILE int pari_errno; \
  if ((pari_errno = setjmp(GP_DATA->env))) { \
	  __keeper.restore();

#define TRY_PARI } else

#define ENDCATCH_PARI  }



#define SET_ERROR if(!PyErr_Occurred()) PyErr_SetString(pari_error, "Error inside PARI module")
#define PRINT_ERROR(errno) printf("Pari error: %s\n", errmessage[errno])

#endif
